<?php 
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/


defined('_JEXEC') or die('Restricted access');

class JobBoardControllerJATransactions extends JobBoardController
{
	function display()
	{
		global $option;
		
		$model =$this->getModel('jatransactions');
		
		$filter=array();
		$search = JRequest::getVar('submitsearch');
		$keyword = JRequest::getVar('keyword','');
		$filter['keyword'] =$keyword;
		$filter['type'] =JRequest::getVar('type','all');
		$userid=JRequest::getVar('userid',null);
		$hidden=JRequest::getVar('hidden',0);
		$limit = JRequest::getVar("limit",0);
		if($limit ==0) $limit=20;
		$limitstart	= JRequest::getVar('limitstart', 0);
		$total = $model-> getTransactions($filter,0,$limitstart,$limit,$userid,$hidden);
		$lists = $model-> getTransactions($filter,1,$limitstart,$limit,$userid,$hidden);
		JRequest::setVar('limit', (int) $limit);
   		jimport('joomla.html.pagination');
    	$pagination = new JPagination($total, $limitstart, $limit);
    	$url = "index.php?option=$option&view=jatransactions&keyword=".$filter['keyword']."&type=".$filter['type'];
		global $mainframe;
	 	$document	= & JFactory::getDocument();
	 	$document->setTitle( JText::_( 'LIST TRANSACTIONS' ) );
		// set view
		$viewType = $document->getType();

		$layout   = 'show';		$viewName = 'jatransactions';
		$view 	  = & $this->getView($viewName,$viewType);
		$view->setLayout($layout);
		$view->assign('lists',$lists);
		$view->assign('hidden',$hidden);
		$view ->assign('type',$filter['type']);
		$view->assign('keyword',$filter['keyword']);
		$view->assign('limit',$limit);
		$view->assign('url',$url);
		$view->assign("pagination",$pagination);
		$view->assign("userid",$userid);
		$view->display();
	}
	function getfilter()
	{
		$filter = array();
		$keyword = JRequest::getVar('keyword','');
		if ($keyword !='')
		{
			$filter['keyword'] =$keyword;
		}
		$filter['type'] =JRequest::getVar('type');
	}
	
	function cancel(){
		global $option;
		$this->setRedirect("index.php?option=$option&view=jatransactions");
	}
	function print_invoice()
	{
		$model = $this->getModel('jatransactions');
		$document = &JFactory::getDocument();
		$viewName	= 'jatransactions';
		$viewType	= $document->getType();
		$view = &$this->getView($viewName, $viewType);
		$layout="print_invoice";
		$view->setLayout($layout);
		$tranid = JRequest::getVar('tranid');
		$transaction = $model->getInvoice($tranid);
		
		if(!$transaction)
		{
			$err ="Not fount invoice";
		}
		else 
		{
			$invoice = $this->parseTrans($transaction);
		}
		
		$view->assign('tranid',$tranid);
		$view->assign('err',$err);
		$view->assign('invoice',$invoice);
		$view->display();

	}
	function  parseTrans($invoice)
	{
		global $sys_invoice;
		
		get_sys_invoice();
		
		$row = new stdClass();
		$row = $invoice;
		$model = $this->getModel('payment');
		$row->user_info =  getUserinfo((int)$invoice->user_id);
		
		
		if($invoice->bill_to !=-1)
		{
			$user_billto = getUserinfo((int)$invoice->bill_to);
		}
		else $user_billto =  $sys_invoice;
		if($invoice->ship_to !=-1)
		{
			$user_shipto = getUserinfo((int)$invoice->ship_to);
		}
		else $user_shipto =  $sys_invoice;
		
		
		$billto_text ="";
		$billto_text .= "<div> $user_billto->username </div>";
		$billto_text .= "<div> Company:  $user_billto->company </div>";
		$billto_text .= "<div> Email:  $user_billto->email </div>";
		$billto_text .= "<div> Phone:  $user_billto->country_code $user_billto->phone_number </div>";
		$billto_text .= "<div> Location:  $user_billto->location </div>";
		$shipto_text="";
		$shipto_text .= "<div> $user_shipto->username </div>";
		$shipto_text .= "<div> Company:  $user_shipto->company </div>";
		$shipto_text .= "<div> Email:  $user_shipto->email </div>";
		$shipto_text .= "<div> Phone:  $user_shipto->country_code $user_shipto->phone_number </div>";
		$shipto_text .= "<div> Location:  $user_shipto->location </div>";
		$row->bill_to_info = $billto_text;
		$row->ship_to_info =$shipto_text;	
		$row->date_create = date("Y-m-d", $invoice->date);
		return $row;
	}
	function view_trans()
	{
		global $option;
				
		$model = &$this->getModel('jatransactions');
		
		$document = &JFactory::getDocument();
		$viewName	= 'jatransactions';
		$viewType	= $document->getType();
		$view = &$this->getView($viewName, $viewType);
		$layout="list_trans";
		$view->setLayout($layout);
		$tranids = JRequest::getVar('cid');
		if(!is_array($tranids)) 
		{
			$tranids = explode(',',$tranids);
		}
		$err = "";
		if($tranids[0]==0)
		{
			$err = JText::_("PLEASE CHOOSE TRANSACTIONS");
		}
		else 
		{
			$invoice = $model->getInvoiceAdmin($tranids);
		}
		
		$ids = implode(',',$tranids);
		$view->assign('ids',$ids);
		$view->assign('err',$err);
		$view->assign('invoice',$invoice);
		$view->display();

	}
	function convert_pdf()
	{
		global $option;
		
		$tranid = JRequest::getVar('tranid');
		$type = JRequest::getVar('type','');
		if(($tranid !=0)&&($type=='invoice'))
		{
			$url  = "index2.php?option=$option&view=jatransactions&task=print_invoice&ajax=1";
			$url .= "&tranid=$tranid&format=pdf";
		}
		else 
		{
			$cid = JRequest::getVar('cid');
			$url  = "index2.php?option=$option&view=jatransactions&task=view_trans&ajax=1";
			$url .= "&cid=$cid&format=pdf";
		}
		$this->setRedirect($url);
	}
	function back_profile()
	{
		global $option;
		
		$userid=JRequest::getVar('userid',null);
		$msg = JText::_( 'OPERRATION CANCELLED' );
		if($userid==null)
			$this->setRedirect( JRoute::_("index.php?option=$option&view=japrofiles"), $msg );
		else {
			$this->setRedirect( JRoute::_("index.php?option=$option&view=japrofiles&task=user&user_id=$userid"), $msg );			
		}
	}
}
?>